package cn.houserent.dao.impl;

import java.util.List;

import org.hibernate.Transaction;

import cn.houserent.dao.BaseDao;
import cn.houserent.dao.HibernateUtil;
import cn.houserent.dao.HouseDao;
import cn.houserent.entity.House;

public class HouseDaoImpl extends BaseDao implements HouseDao{

	@Override
	public List<House> getList(House house,int pageNo,int pageSize) {
		StringBuffer sql = new StringBuffer("from House where 1=1");
		if(house != null){
			if(house.getPrice() != null && house.getPrice() != 0){
				sql.append(" and price = :price");
			}
			if(house.getTitle() != null && house.getTitle() != ""){
				sql.append(" and title = :title");
			}
			if(house.getPubdate() != null ){
				sql.append(" and pubdate = :pubdate");
			}
			if(house.getContact() != null && house.getContact() != ""){
				sql.append(" and contact = :contact");
			}
			return currentSession().createQuery(sql.toString()).setProperties(house).
					setFirstResult((pageNo-1)*pageSize).setMaxResults(pageSize).list();
		}else{
			return currentSession().createQuery(sql.toString()).
					setFirstResult((pageNo-1)*pageSize).setMaxResults(pageSize).list();
		}
	}

	@Override
	public Long countHouse(House house) {
		StringBuffer sql = new StringBuffer("select count(id) from House where 1=1");
		if(house != null){
			if(house.getPrice() != null && house.getPrice() != 0){
				sql.append(" and price = :price");
			}
			if(house.getTitle() != null && house.getTitle() != ""){
				sql.append(" and title = :title");
			}
			if(house.getPubdate() != null ){
				sql.append(" and pubdate = :pubdate");
			}
			if(house.getContact() != null && house.getContact() != ""){
				sql.append(" and contact = :contact");
			}
			return (Long) currentSession().createQuery(sql.toString()).setProperties(house).
					uniqueResult();
		}else{
			return (Long) currentSession().createQuery(sql.toString()).uniqueResult();
		}
	}
	
}
